<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">{{title}}</h3>
      <el-form-item prop="username" v-if="!isSnLogin">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="password" v-if="!isSnLogin">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-checkbox
        v-if="!isSnLogin"
        v-model="loginForm.rememberMe"
        style="margin: 0px 0px 25px 0px"
        >记住密码</el-checkbox
      >
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">{{isSnLogin ? '数 字 证 书 登 录':'登 录'}}</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>{{copyright}}</span>
    </div>
  </div>
</template>

<script>
import { login ,snLogin} from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        // password: "admin123",
        rememberMe: false,
        code: "",
        uuid: "",
        sn:""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
        code: [
          { required: true, trigger: "change", message: "验证码不能为空" },
        ],
      },
      loading: false,
      redirect: undefined,
      title:window.g.VUE_APP_NAME,
      copyright:window.g.VUE_APP_COPYRIGHT,
      isSnLogin:false
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    let isLocal = window.location.href.indexOf('127.0.0.1') > -1
    let Sn = window.g.LOGIN_MODEL === 2

    if(Sn && isLocal){
      this.isSnLogin = false;
    }else if (Sn){
      this.isSnLogin = true;
    }

    // this.getCode();
    this.getCookie();
  },
  mounted(){
    // let isLocal = window.location.href.indexOf('127.0.0.1') > -1
    // console.log('登录日志----start')
    // console.log('登录模式',window.g.LOGIN_MODEL)
    // console.log('是否127',isLocal)
    // if(window.g.LOGIN_MODEL !== 1 && !isLocal){
    //   this.loginForm.sn = Cookies.get("KOAL_CERT_CN")
    //   console.log('Cookies-sn',this.loginForm.sn)
    //   if(!!this.loginForm.sn){
    //     this.loginForm.username = ""
    //     this.loginForm.password = ""
    //     this.requireLogin()
    //     console.log('获取到sn自动登录')
    //   }
    // }
    // if(window.g.LOGIN_MODEL == 2 && !isLocal){
    //   let sn = Cookies.get("KOAL_CERT_CN")
    //   console.log('sn',sn)
    //   if(!sn){
    //     this.msgError('请插入U盾')
    //   }
    // }
    // console.log('登录日志----end')
  },
  methods: {
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {

      this.$router.push({ path: "/index" });
      // if(this.isSnLogin){
      //   snLogin().then((res)=>{
      //     if (res.code == "200") {
      //       localStorage.setItem("userId", res.data.user);
      //       localStorage.setItem("isAdmin", res.data.super);
      //       //设置过期时间
      //       var expires_time = new Date(new Date().getTime() + window.g.COOKIES_EXP * 60 * 1000);
      //       Cookies.set("loginuser", res.data.user, { expires: expires_time });
      //       this.$router.push({ path: "/index" });
      //     }
      //   })
      //   return
      // }
      // this.$refs.loginForm.validate((valid) => {
      //   if (valid) {
      //     if (this.loginForm.rememberMe) {
      //       Cookies.set("username", this.loginForm.username, { expires: 30 });
      //       Cookies.set("password", encrypt(this.loginForm.password), {
      //         expires: 30,
      //       });
      //       Cookies.set("rememberMe", this.loginForm.rememberMe, {
      //         expires: 30,
      //       });
      //     } else {
      //       Cookies.remove("username");
      //       Cookies.remove("password");
      //       Cookies.remove("rememberMe");
      //     }
      //     this.requireLogin()
      //   }
      // });
    },
    requireLogin(){
      this.loading = true;
      login(this.loginForm.username, this.loginForm.password,this.loginForm.sn).then(
        (res) => {
          if (res.code == "200") {
            localStorage.setItem("userId", res.data.user);
            localStorage.setItem("isAdmin", res.data.super);
            //设置过期时间
            var expires_time = new Date(new Date().getTime() + window.g.COOKIES_EXP * 60 * 1000);
            Cookies.set("loginuser", res.data.user, { expires: expires_time });
            this.$router.push({ path: "/index" });
          } else {
          }
        }
      ).finally(()=>{
          this.loading = false;
      });
    }
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-image: url("../assets/images/back.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -200px;
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  margin-top: -110px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
